package com.sdu.cloudservice.dao;

import com.sdu.cloudservice.entity.PredictPriceEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Create by lxf on 19-07-01.
 * @author
 * LXF
 */
@Mapper
@Repository
public interface PredictPriceDao {
    /**
     * 分两个sql，方便在service层中提早结束——没有或没有完全预测的
     * 根据可用区id和实例id和endtime找到“刚好大点的”
     * @param zoneId
     * @param instanceId
     * @return
     */
    @Select("SELECT * FROM predict_price WHERE zone_id=#{zoneId} and instance_id=#{instanceId} and alg_type=#{algType} and time=#{time}")
    public PredictPriceEntity getExactlyPredictPrice(@Param("zoneId") String zoneId, @Param("instanceId") String instanceId,
                                                     @Param("time") String time, @Param("algType") int algType);

    @Select("SELECT * FROM predict_price WHERE zone_id=#{zoneId} and instance_id=#{instanceId} and alg_type=#{algType} and time<#{time} ORDER BY time DESC LIMIT 1")
    PredictPriceEntity getPredictPriceSmaller(@Param("zoneId") String zoneId, @Param("instanceId") String instanceId,
                                                            @Param("time") String time, @Param("algType") int algType);

    @Select("SELECT * FROM predict_price WHERE zone_id=#{zoneId} and instance_id=#{instanceId} and alg_type=#{algType} and time>#{time} ORDER BY time ASC LIMIT 1")
    public PredictPriceEntity  getPredictPriceBigger(@Param("zoneId") String zoneId, @Param("instanceId") String instanceId,
                                                     @Param("time") String time, @Param("algType") int algType);

    @Select("SELECT * FROM predict_price WHERE zone_id=#{zoneId} and instance_id=#{instanceId} " +
            "and alg_type=#{algType} and time<=#{endTime} and time >=#{startTime} ORDER BY time ASC")
    public List<PredictPriceEntity> getPredictPrice(@Param("zoneId") String zoneId, @Param("instanceId") String instanceId,
                                                    @Param("startTime") String startTime, @Param("endTime") String endTime,
                                                    @Param("algType") int algType);

    @Delete("DELETE FROM predict_price WHERE zone_id=#{zoneId} and instance_id=#{instanceId} and alg_type=#{algType} " +
            "and time<=#{endTime} and time>=#{startTime}")
    public int delPredictPrices(@Param("zoneId") String zoneId, @Param("instanceId") String instanceId,
                               @Param("startTime") String startTime, @Param("endTime") String endTime,
                               @Param("algType") int algType);

    @Delete("DELETE FROM predict_price WHERE zone_id=#{zoneId} and instance_id=#{instanceId} and alg_type=#{algType} " +
            "and time=#{time}")
    public int delPredictPrice(@Param("zoneId") String zoneId, @Param("instanceId") String instanceId,
                               @Param("time") String time, @Param("algType") int algType);

    @Insert("INSERT INTO predict_price VALUES(#{zoneId},#{instanceId},#{time},#{spot},#{algType},#{status})")
    public int insertIntoPredictPrice(@Param("zoneId")String zoneId,@Param("instanceId")String instanceId,
                                      @Param("time")String time,@Param("spot")double spot,
                                      @Param("algType")int algType,@Param("status")int status);
    @Update("UPDATE predict_price SET status=#{status} WHERE zone_id=#{zoneId} and instance_id=#{instanceId} and time=#{time} " +
            "and alg_type=#{algType}")
    public int updateStatus(@Param("status")int status,@Param("zoneId")String zoneId,
                                @Param("instanceId")String instanceId,@Param("time")String time,@Param("algType") int algType);
}
